package com.css.fxfzxczh.modules.aftershockSequence.repository.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.aftershockSequence.entity.AftershockSequence;
import com.css.fxfzxczh.modules.aftershockSequence.repository.AftershockSequenceRepository;
import com.css.fxfzxczh.modules.aftershockSequence.vo.AftershockSequenceVO;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author:
 * @CreateTime: 2024/4/7 15:07
 * @Description:
 */
@Repository
public class AftershockSequenceRepositoryImpl implements AftershockSequenceRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void deleteAll() {
        String sql = " delete from xczh_aftershock_sequence ";
        jdbcTemplate.update(sql);
    }

    @Override
    public void batchSave(LinkedList<AftershockSequence> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql = "insert into xczh_aftershock_sequence values(:id,:cataId,:earthquakeDate,:mm," +
                ":latitude,:longitude,:epiDepth,:autoFlag,:location,:province,:city,:county,:time,ST_GeometryFromText(:geom,4490))";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public List<AftershockSequence> getAftershockSequenceData(AftershockSequenceVO aftershockSequenceVO) {
        StringBuilder sql = new StringBuilder("select * from xczh_aftershock_sequence where 1=1 ");
        if (aftershockSequenceVO.getStartTime() != null && aftershockSequenceVO.getEndTime() != null) {
            sql.append(" and Date(earthquake_date) between '"+aftershockSequenceVO.getStartTime()+"' and '"+aftershockSequenceVO.getEndTime()+"'");
        }
        List<AftershockSequence> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(AftershockSequence.class));
        return list;
    }

    @Override
    public Boolean whetherInTheSixIntensityCircle(String xsemId, String id) {

        String sql = "select ST_Within(\n" +
                "(select st_astext(geom) from xczh_aftershock_sequence where id= ? ), \n" +
                "(select st_astext(st_minimumboundingcircle((select st_union(geom) \n" +
                "from xzch_draw_seismic_influence where intensity='6' and del_flag='0' and xsem_id = ? ))) as circle))";

        String result = jdbcTemplate.queryForObject(sql, new Object[]{id, xsemId}, String.class);
        return "t".equals(result) ? true : false;

    }

    @Override
    public Integer getAftershockSequenceByCondition(String eventTime, String magnitude1, String magnitude2) {
        StringBuilder sql = new StringBuilder("select count(id) from xczh_aftershock_sequence where 1=1 ");
        if (PlatformObjectUtils.isNotEmpty(eventTime)) {
            sql.append(" and Date(earthquake_date) <= '"+eventTime+"'");
        }
        if(PlatformObjectUtils.isNotEmpty(magnitude1)){
            sql.append(" and mm >= '"+magnitude1+"'");
        }
        if(PlatformObjectUtils.isNotEmpty(magnitude2)){
            sql.append(" and mm <= '"+magnitude2+"'");
        }
        return jdbcTemplate.queryForObject(sql.toString(), Integer.class);
    }
}
